package liaotian;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;

public class ServerReceive implements Runnable{
	private Socket socket;
	private ChatRecord<String> chatRecord;
	private List<Socket> listSocket;
	
	
	@Override
	public void run() {
		try {
			
			while(true){
				ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
				String o=(String)ois.readObject();
				System.out.println(listSocket.size()+"---");
				sendMess(o);
//				chatRecord.add(o);				
			}
		} catch (Exception e) {					
			listSocket.remove(socket);
			sendMess(socket.getInetAddress()+"离开了");
		}
	}
	
	private void sendMess(String o){
		try {
			for (Socket socket : listSocket) {
				ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
				oos.writeObject(o);
				oos.flush();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ServerReceive(Socket socket, ChatRecord<String> chatRecord, List<Socket> listSocket) {
		this.socket = socket;
		this.chatRecord = chatRecord;
		this.listSocket = listSocket;
	}
}
